/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.fjut.se1603.wwm.service.impl;

import edu.fjut.se1603.wwm.DAO.impl.AccountDaoImpl;
import edu.fjut.se1603.wwm.DAO.impl.PetDaoImpl;
import edu.fjut.se1603.wwm.entity.Pet;
import edu.fjut.se1603.wwm.entity.PetOwner;
import edu.fjut.se1603.wwm.service.PetOwnerService;
import edu.fjut.se1603.wwm.DAO.impl.PetOwnerDaoImpl;
import edu.fjut.se1603.wwm.DAO.impl.PetStoreDaoImpl;
import edu.fjut.se1603.wwm.entity.Account;
import edu.fjut.se1603.wwm.entity.Pet;
import edu.fjut.se1603.wwm.entity.PetOwner;
import edu.fjut.se1603.wwm.entity.PetStore;
import edu.fjut.se1603.wwm.service.PetOwnerService;
import java.io.UnsupportedEncodingException;
import java.security.acl.Owner;
import java.sql.Date;
import java.util.List;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Daydream
 */
public class PetOwnerServiceImpl implements PetOwnerService {

    @Override
    public void buy(Pet pet, PetOwner owner) {
        PetDaoImpl pdi = new PetDaoImpl();
        PetOwnerDaoImpl podi = new PetOwnerDaoImpl();
        PetStoreDaoImpl psdi = new PetStoreDaoImpl();
        AccountDaoImpl adi = new AccountDaoImpl();
        Account account = new Account();

        Date d = new Date(new java.util.Date().getTime());
        account.setBuyer_id(owner.getId());
        account.setPet_id(pet.getId());
        account.setSeller_id(pet.getStore_id());
        account.setDeal_type(1);
        account.setPrice(pet.getPrice());
        account.setDate_time(d);

        adi.add(account);
        psdi.change(pet.getStore_id(), pet.getPrice(), 1);
        podi.change(owner, pet.getPrice(), 0);
        pdi.change(pet, owner.getId(), 0);
        System.out.println("台账正确插入一条信息");
    }

    @Override
    public void sell(Pet pet, PetOwner owner) {
        PetDaoImpl pdi = new PetDaoImpl();
        PetOwnerDaoImpl podi = new PetOwnerDaoImpl();
        PetStoreDaoImpl psdi = new PetStoreDaoImpl();
        AccountDaoImpl adi = new AccountDaoImpl();

        System.out.println("--------您要卖出的宠物信息如下--------");
        System.out.println("宠物名字叫：" + pet.getName() + " 宠物类别是：" + pet.getTypename());
        System.out.println("请确认是否卖出，y代表卖出，n代表不卖");
        Scanner sc = new Scanner(System.in);
        String sc1 = sc.nextLine();
        if (sc1.equals("y")) {
            System.out.println("--------下面是现有宠物商店，请选择您要卖给买家序号--------");
            List<PetStore> petStore = psdi.getList();
            PetStore[] petStores = new PetStore[100];
            int num = 0;
            for (int i = 0; i < petStore.size(); i++) {
                if (petStore.get(i).getId() != 0) {
                    System.out.println("序号：" + (num + 1) + " 宠物商店的名字是：" + petStore.get(i).getName());
                    petStores[num] = petStore.get(i);
                    num++;
                }
            }
            int a = (sc.nextInt() - 1);
            PetStore ps = petStores[a];

            Account account = new Account();

            Date d = new Date(new java.util.Date().getTime());
            account.setBuyer_id(ps.getId());
            account.setPet_id(pet.getId());
            account.setSeller_id(owner.getId());
            account.setDeal_type(2);
            account.setPrice(pet.getPrice());
            account.setDate_time(d);

            adi.add(account);
            psdi.change(pet.getStore_id(), pet.getPrice(), 0);
            podi.change(owner, pet.getPrice(), 1);
            pdi.change(pet, 0, ps.getId());
            System.out.println("台账正确插入一条信息");
        } else if (sc1.equals("n")) {
            System.out.println("交易取消！");
        } else {
            System.out.println("无效输入！");
        }
    }

    @Override
    public PetOwner login() {
        Scanner sc = new Scanner(System.in);

        PetOwnerDaoImpl owner = new PetOwnerDaoImpl();
        List<PetOwner> owners = owner.getList();
        PetOwner powner = new PetOwner();
        String owner_name;
        String owner_password;

        System.out.println("请先登陆，请您输入主人的名字：");
        owner_name = sc.nextLine();
        System.out.println("请您输入主人密码：");
        owner_password = sc.nextLine();

        for (int j = 0; j < owners.size(); j++) {
            if ((owner_name.equals(owners.get(j).getName())) && (owner_password.equals(owners.get(j).getPassword()))) {
                System.out.println("--------恭喜您成功登录--------");
                powner.setId(owners.get(j).getId());
                powner.setName(owner_name);
                powner.setPassword(owner_password);
                powner.setMoney(owners.get(j).getMoney());
                System.out.println("--------您的基本信息：--------");
                System.out.println("名字：" + owner_name);
                System.out.println("元宝数：" + powner.getMoney());
                break;
            } else {
                if (j == owners.size() - 1) {
                    System.out.println("登录失败！请确认用户名和密码后重新输入！");
                    this.login();
                }
            }
        }
        return powner;
    }

}
